import os

from client.pages.ChatPage import *
from client.pages.GroupInfoWin import *
from client.pages.GroupEditPage import GroupEditPage
from client.func.ClientHttp import group_application_post, get_group_info_post, delete_group_post, out_group_post


root = "./"
img_dir = "client/.cache/img_buf/"


class GroupInfoPage(QMainWindow, Ui_GroupInfoWin):
    def __init__(self):
        super(GroupInfoPage, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.myuid = ""
        self.is_my_group = False
        self.is_master = False
        self.uid = ""
        self.img_path = "./client/.cache/img_buf/group_default.png"
        self.explain = ""
        self.name = u"群聊"

        self.close_button.clicked.connect(self.OnClose)
        self.chat_button.clicked.connect(self.OnChat)
        self.edit_button.clicked.connect(self.OnEdit)
        self.delete_button.clicked.connect(self.OnDelete)

    def OnDelete(self):
        if self.is_my_group:
            if self.is_master:
                res = QMessageBox.question(self, u'删除群聊', u'是否确定删除此群聊？', QMessageBox.Yes | QMessageBox.No)
                if res == QMessageBox.Yes:
                    delete_group_post(self.myuid, self.uid)
                    self.close()
            else:
                res = QMessageBox.question(self, u'退出群聊', u'是否确定退出此群聊？', QMessageBox.Yes | QMessageBox.No)
                if res == QMessageBox.Yes:
                    out_group_post(self.myuid, self.uid)
                    self.update()

    def OnEdit(self):
        self.edit_win = GroupEditPage()
        self.edit_win.myuid = self.myuid
        self.edit_win.uid = self.uid
        self.edit_win.update()
        self.edit_win.show()

    def OnChat(self):
        if self.is_my_group:
            self.chat_win = ChatPage()  # 聊天框
            self.chat_win.myuid = self.myuid
            self.chat_win.target = self.uid
            self.chat_win.category = "group"
            self.chat_win.init_update()
            self.chat_win.show()
        else:
            if group_application_post(self.myuid, self.uid):
                self.chat_button.setText(u"加群申请已发送")

    def calc_relationship(self):
        if self.is_my_group:
            self.chat_button.setText(u"聊天")
            self.delete_button.show()
            if self.is_master:
                self.edit_button.show()
                self.delete_button.setText(u"删除群聊")
            else:
                self.edit_button.hide()
                self.delete_button.setText(u"退出群聊")
        else:
            self.chat_button.setText(u"加入群聊")
            self.edit_button.hide()
            self.delete_button.hide()

    def update(self):
        try:
            info = get_group_info_post(self.uid)
            if info:
                self.name = info["name"]
                self.img_path = root + img_dir + info["img"]
                self.explain = info["explain"]
                self.is_master = (self.myuid == info["master"])
                self.is_my_group = (self.myuid in info["members"])
                self.set_img()
                self.calc_relationship()
                self.name_label.setText(self.name)
                self.uid_label.setText("uid: " + self.uid)
                self.textEdit.setText(self.explain)
        except Exception as e:
            print(e)

    def set_img(self):
        if os.path.exists(self.img_path):
            img = QtGui.QPixmap(self.img_path).scaled(self.img_label.width(), self.img_label.height())
            self.img_label.setPixmap(img)

    def OnClose(self):
        self.close()


